<template>
  <div class="procure-entry-detail">
    <div class="info">
      <div>
        <div class="left">
          <p class="creater mr40">创建人：{{ detail.createBy || "-" }}</p>
          <p class="mr40">制单时间：{{ detail.createTime || "-" }}</p>
          <p class="mr40">
            入库类型：{{
              detail.type === 1
                ? "采购入库"
                : detail.type === 2
                  ? "仓库调拨"
                  : detail.type === 3
                    ? "销售退货"
                    : detail.type === 4
                      ? "其他入库"
                      : "-"
            }}
          </p>
          <p class="mr40">关联编码：{{ detail.associateCode || "-" }}</p>
          <p>入库订单编码：{{ detail.inboundNo || "-" }}</p>
        </div>
        <div class="right">
          <!-- <el-button type="info" plain @click="$router.push({ path: '/procure/entry', query: {} })">返回</el-button> -->
        </div>
      </div>
    </div>

    <el-container class="content">
      <el-aside>
        <header>
          <h2>
            入库仓：<b>{{ detail.warehouseName || "-" }}</b>
          </h2>
        </header>

        <!-- 商品列表 -->
        <div class="products-list" style="overflow-x: auto">
          <div class="my-table" style="min-width: 1300px">
            <div class="table-header">
              <div>
                <div>
                  <div class="col1">商品信息</div>
                  <div class="col2">数量</div>
                  <div class="col3">预计入库件数</div>
                  <div class="col4">单位</div>
                  <div class="col5">实际入库数量</div>
                  <div class="col6">实际入库件数</div>
                </div>
              </div>
            </div>

            <div class="table-body" v-if="list.length > 0">
              <div class="list-item" v-for="(item, index) in list" :key="index">
                <div>
                  <div class="col1">
                    <div>
                      <div class="left">
                        <img :src="item.mainPicUrl" alt="" />
                      </div>
                      <div class="right">
                        <h3 class="name text-break">{{ item.productName || "-" }}</h3>
                        <p style="white-space: break-spaces">
                          <!-- {{ getSpes(item.skuProperties) }} -->
                          {{ item.skuProperties }}
                        </p>
                        <p>SKU编码：{{ item.skuCode || "-" }}</p>
                        <p>ID:{{ item.productId || "-" }}</p>
                      </div>
                    </div>
                  </div>
                  <div class="col2">{{ item.deliveryQuantity }}</div>
                  <div class="col3">
                    <!-- <el-input v-model="item.yjNum" oninput="value=value.replace(/[^0-9]/g, '')"></el-input> -->
                    <span>{{ item.deliveryPartQuantity }}</span>
                  </div>
                  <div class="col4">
                    <!-- {{ item.productUnit }} -->
                    {{ getName(item.productUnit) || "-" }}
                  </div>
                  <div class="col5">
                    <!-- <el-input v-model="item.inventoryQuantity" oninput="value=value.replace(/[^0-9]/g, '')"></el-input> -->
                    <span>{{ item.inventoryQuantity }}</span>
                  </div>
                  <div class="col6">
                    <!-- <el-input v-model="item.inventoryPartQuantity" oninput="value=value.replace(/[^0-9]/g, '')"></el-input> -->
                    <span>{{ item.inventoryPartQuantity }}</span>
                  </div>
                </div>
                <div class="tips">
                  <p class="label">备注</p>
                  <!-- <el-input maxlength="200" v-model="item.remark" placeholder="请输入"></el-input> -->
                  <RemarkEditor v-model="item.remark" :isEdit="false" />
                </div>
                <div class="tips tips2" v-if="item.exceptionType">
                  <p style="color: #ff3333">异常处理：{{ item.exceptionType === 1 ? "漏发待补发" : "改版" }}</p>
                  <p class="ml20">异常梳理备注：{{ item.exceptionCause || "-" }}</p>
                </div>
              </div>
            </div>
            <div v-else style="display: flex; justify-content: center; width: 100%">
              <el-empty description="暂无数据" />
            </div>
          </div>
        </div>
      </el-aside>
      <el-main width="300px">
        <!-- 客户信息-与下拉选择的收货仓库联动 -->
        <div class="kh-info">
          <h3>客户信息</h3>
          <div class="mb20">
            <p>姓名：</p>
            <b>{{ detail.receivingName || "-" }}</b>
          </div>
          <div class="mb20">
            <p>电话：</p>
            <b>{{ detail.receivingMobile || "-" }}</b>
          </div>
          <div class="mb20">
            <p>地址：</p>
            <b>{{ detail.receivingAddress || "-" }}</b>
          </div>
          <div>
            <p>来往单位：</p>
            <b>{{ detail.anotherName || "-" }}</b>
          </div>
        </div>
        <div class="divider"></div>

        <!-- 关联借调客户信息 -->
        <!-- <SecondCustomer
          ref="secondCustomerRef"
          :isEdit="false"
          :isEditRemark="false"
          :detail="detail"
          title="关联客户信息"
          remarkTitle="关联备注说明"
          dialogTitle="添加关联客户"
          actionTips="确定要取消关联客户信息？"
        />
        <div class="divider"></div> -->

        <!-- 物流信息 -->
        <div class="logis-wrap">
          <header>
            <p>物流信息</p>
            <el-button type="primary" link size="small" @click="dialogVisible = true">查看全部</el-button>
          </header>
          <div class="logis-info">
            <h2>物流编码：{{ detail.logisticsTrackNo || "-" }}</h2>
            <el-divider />

            <el-timeline style="max-width: 600px" v-if="activities.length > 0">
              <el-timeline-item v-for="(activity, index) in activities" :key="index">
                <template #default>
                  <div class="left">
                    <p>{{ activity.time.split(" ")[0] || "-" }}</p>
                    <i>{{ activity.time.split(" ")[1] || "-" }}</i>
                  </div>
                  <div class="right">
                    <p>{{ activity.context || "-" }}</p>
                  </div>
                </template>
                <!-- {{ activity.content }} -->
              </el-timeline-item>
            </el-timeline>
            <div v-else style="display: flex; justify-content: center; width: 100%">
              <el-empty description="这里暂时什么都没有" />
            </div>
          </div>
        </div>
        <div class="divider"></div>

        <!-- 入库备注 -->
        <div class="rk-tips">
          <p class="label">入库备注</p>
          <RemarkEditor v-model="detail.remark" :isEdit="true" @submit="saveRemarkFun" @cancel="cancelRemarkFun" />
        </div>
      </el-main>
    </el-container>
    <!-- 查看全部物流信息弹窗 -->
    <el-dialog v-model="dialogVisible" title="物流信息" width="500">
      <div class="dialog-content">
        <div class="file-wrap">
          <p>物流凭证：</p>
          <div class="code-wrap">
            <div class="code mb10 ml10" v-for="(file, index) in fileUrls" :key="file">
              <img :src="getIcon(file)" @click="download(file)" />
            </div>
          </div>
        </div>

        <el-timeline style="max-width: 600px" v-if="activities.length > 0">
          <el-timeline-item v-for="(activity, index) in activities" :key="index">
            <template #default>
              <div class="left">
                <p>{{ activity.time.split(" ")[0] || "-" }}</p>
                <i>{{ activity.time.split(" ")[1] || "-" }}</i>
              </div>
              <div class="right">
                <p>{{ activity.context || "-" }}</p>
              </div>
            </template>
            <!-- {{ activity.content }} -->
          </el-timeline-item>
        </el-timeline>
        <div v-else style="display: flex; justify-content: center; width: 100%">
          <el-empty description="这里暂时什么都没有" />
        </div>
      </div>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="dialogVisible = false"> 我知道了 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, nextTick, onMounted } from "vue";
import SearchForm from "@/components/SearchForm";
import { Search, RefreshRight } from "@element-plus/icons-vue";
import { inboundOrderDetail, findOrder, saveRemark } from "@/apis/procure/entry";
import { useRoute } from "vue-router";
const route = useRoute();
import useCodeArrStore from "@/store/modules/select";
import RemarkEditor from "@/components/RemarkEditor";
import word from "@/assets/images/word.png";
import pdf from "@/assets/images/pdf.png";
import video from "@/assets/images/video.png";
import excel from "@/assets/images/excel.png";
import { ElMessage } from "element-plus";
import SecondCustomer from "@/views/procure/outbound/components/secondCustomer.vue";

const inboundId = ref(route.query.inboundId);
const list = ref([]);
const detail = ref({});
const activities = ref([]);
const fileUrls = ref([]);
const dialogVisible = ref(false);

onMounted(() => {
  getList();
});

//取消保存备注信息
const cancelRemarkFun = () => {
  console.log("这是原始的备注信息===", _remark.value);
};

//及时保存备注信息
const saveRemarkFun = () => {
  console.log("这是修改之后的备注信息===", detail.value.remark);
  saveRemark({ inboundId: inboundId.value, remark: detail.value.remark })
    .then(res => {
      if (res.code === 200) {
        ElMessage({
          message: "备注信息保存成功~",
          type: "success"
        });
      }
    })
    .finally(() => {
      getList();
    });
};

const getName = code => {
  return useCodeArrStore().getDictName("shop_unit", code);
};

const download = url => {
  const link = document.createElement("a");
  link.href = url;
  link.click();
};

/**
 * 根据类型返回相应图标
 */
const getIcon = url => {
  let icon = "";
  if (getType(url) == "image") icon = url;
  else icon = icons[getType(url)];
  return icon;
};
const fileTypes = {
  image: ["jpg", "png", "jpeg", "gif"],
  video: ["mp4", "avi"],
  excel: ["xls", "xlsx"],
  word: ["doc", "docx"],
  pdf: ["pdf"]
};
const icons = { word, pdf, excel, video };
/**
 * 获取文件类型
 */
const getType = url => {
  let type = "";
  for (let key in fileTypes) {
    fileTypes[key]?.filter(v => {
      if (url.indexOf(v) != -1) {
        type = key;
      }
    });
  }
  return type;
};

//查询详情数据
const _remark = ref(""); //存一份备注信息，以防取消编辑
const getList = () => {
  inboundOrderDetail({ inboundId: inboundId.value }).then(async res => {
    if (res.code === 200) {
      list.value = res.data.orderGoodsInfoList;
      detail.value = res.data;
      _remark.value = JSON.parse(JSON.stringify(res.data.remark));

      getLogisDetail(
        detail.value.logisticsCompanyNo, //物流公司code
        detail.value.logisticsTrackNo, //物流单号
        detail.value.productPurchasePackage //手机号
      );
    }
  });
};

//查询商品详情接口
const getLogisDetail = async (com, num, phone) => {
  if (com && num) {
    await findOrder({ com, num, phone }).then(res => {
      if (res.code === 200) {
        activities.value = res.data.data;
        fileUrls.value = res.data.logisticsCertificates ? res.data.logisticsCertificates.split(",") : [];
      }
    });
  } else {
    activities.value = [];
    fileUrls.value = [];
  }
};

//拼接商品的sku规格
const getSpes = properties => {
  if (!properties) return "-";
  const arr = JSON.parse(properties) || [];
  if (arr.length > 1) {
    const str = arr.reduce((a, b) => {
      return a.specificationVal + "+" + b.specificationVal;
    });
    return str;
  } else if (arr.length === 1) {
    return arr[0].specificationVal;
  } else {
    return "-";
  }
};
</script>

<style lang="scss" scoped>
@import "./index.scss";
.divider {
  border-bottom: 1px solid #e2e8f0;
  margin: 20px -20px;
}
.dialog-content {
  max-height: 550px;
  overflow-y: auto;
  .file-wrap {
    display: flex;
    p {
      width: 80px;
      padding-top: 20px;
    }
    .code-wrap {
      flex: 1;
      display: flex;
      flex-wrap: wrap;
      img {
        width: 80px;
        height: 80px;
      }
    }
  }
  :deep(.el-timeline) {
    padding-left: 25%;
    .el-timeline-item__wrapper {
      padding-left: 18px;
      top: 0 !important;
    }
    .el-timeline-item__node--normal {
      height: 8px;
      left: 1px;
      width: 8px;
    }
    .left {
      width: 100px;
      // border: 1px solid #eee;
      position: absolute;
      left: -110px;
      text-align: right;
      font-size: 12px;
    }
    .right {
      width: 280px;
      word-break: break-all;
      // border: 1px solid #eee;
    }
  }
}

.code {
  width: 80px;
  height: 80px;
  background: #f2f3f5;
  position: relative;

  &:hover {
    .del {
      display: flex;
    }
  }

  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
}

.procure-entry-detail {
  height: calc(100% - 20px);
  overflow-y: auto;
  padding: 0 20px 20px 20px;
  position: relative;

  .search-wrap {
    padding: 20px;
    overflow-x: hidden;
    background-color: #f6f8fa;
  }

  .info {
    position: sticky;
    top: 0;
    background-color: white;
    z-index: 22;
    & > div {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 14px 0;
      border-bottom: 1px solid #e2e8f0;
    }
    & > div > div {
      display: flex;
    }
    .left {
      .creater {
        color: #1a1a1a;
        font-weight: bold;
      }
    }
  }
  .content {
    height: calc(100% - 75px);
    :deep(.el-aside) {
      flex: 1;
      padding: 20px 20px 20px 0;
      header {
        display: flex;
        justify-content: space-between;
        margin-bottom: 18px;
        h2 {
          font-size: 18px;
          color: #86909c;
          b {
            color: #3d3d3d;
          }
        }
      }
      .products-list {
        .table-header {
          background-color: #f2f6fc;
          margin-bottom: 10px;
        }
        .table-body {
          .list-item {
            margin-bottom: 10px;
            & > div {
              border: 1px solid #e1e6ed;
              display: flex;
              // align-items: center;
              &:not(:first-of-type) {
                border-left: none;
              }
              &:not(:last-of-type) {
                & > div:not(:last-of-type) {
                  border-right: 1px solid #e1e6ed;
                }
              }
              & > div {
                display: flex;
                align-items: center;
              }
            }
            .col1 {
              & > div {
                display: flex;
              }

              .left {
                img {
                  width: 80px;
                  height: 80px;
                  border-radius: 4px;
                }
              }
              .right {
                padding-left: 18px;
                .name {
                  font-size: 14px;
                  color: #3d3d3d;
                  margin-bottom: 5px;
                }
                p {
                  font-size: 12px;
                  color: #86909c;
                  margin-bottom: 5px;
                }
              }
            }
            .tips {
              display: flex;
              padding: 16px 14px;
              border-top: none;
              border-left: 1px solid #e1e6ed !important;
              p.label {
                width: 38px;
              }
              .el-input {
                flex: 1;
                .el-input__wrapper {
                  background-color: #f2f3f5;
                }
              }
            }
            .tips2 {
              p {
                width: auto;
              }
            }
          }
        }
        .table-header,
        .table-body {
          & > div > div:first-of-type {
            display: flex;
            justify-content: space-between;
            & > div {
              padding: 14px 20px;
              &.col1 {
                width: 45%;
              }
              &:not(:first-of-type) {
                flex: 1;
              }
            }
          }
        }
      }
    }
    :deep(.el-main) {
      width: 300px;
      flex: none;
      background-color: #f6f8fa;
      border-left: 1px solid #e2e8f0;
      .el-empty {
        padding-top: 0;
      }
      .sh-store {
        display: flex;
        align-items: center;
        p {
          width: 66px;
        }
        .el-select {
          flex: 1;
        }
      }
      .kh-info {
        h3 {
          color: #86909c;
          font-weight: normal;
          margin: 0 0 16px 0;
        }
        p {
          color: #86909c;
          margin-bottom: 10px;
        }
        b {
          font-weight: normal;
          color: #1a1a1a;
        }
      }
      .logis-wrap {
        header {
          display: flex;
          justify-content: space-between;
          font-size: 16px;
          color: #86909c;
          margin-bottom: 10px;
        }
        .logis-info {
          background-color: white;
          h2 {
            font-size: 12px;
            line-height: 24px;
            padding: 12px 10px;
          }
          .el-divider {
            margin-top: 0;
          }
          .el-timeline {
            padding-left: 40%;
            max-height: 200px;
            overflow-y: auto;
            .el-timeline-item__wrapper {
              padding-left: 18px;
              top: 0 !important;
            }
            .el-timeline-item__node--normal {
              height: 8px;
              left: 1px;
              width: 8px;
            }
            .left {
              width: 100px;
              // border: 1px solid #eee;
              position: absolute;
              left: -110px;
              text-align: right;
              font-size: 12px;
            }
            .right {
              width: 100px;
              word-break: break-all;
              // border: 1px solid #eee;
            }
          }
        }
      }
      .rk-tips {
        p.label {
          color: #4e5969;
          margin-bottom: 16px;
        }
      }
    }
  }
  .action-wrap {
    position: absolute;
    bottom: 0;
    right: 0;
    left: 0;
    height: 64px;
    background-color: white;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    padding: 0 20px;
    box-shadow: 0px -3px 10px 0px rgba(4, 24, 48, 0.1);
  }
}
</style>
